package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ColorUtils;


/**
 * Created by 贺玉琨 on 2019/3/16.
 */
public class GradientRoundCornerTextView extends RoundCornerTextView {
    protected int secondaryBackColor;
    protected GradientDrawable.Orientation gradientOrientation;

    public GradientRoundCornerTextView(Context context) {
        this(context, null);
    }

    public GradientRoundCornerTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientRoundCornerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBackground(attrs);
        refreshBackground();
    }

    protected void initBackground(AttributeSet attrs) {
        super.initBackground(attrs);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GradientRoundCornerTextView);
        secondaryBackColor = a.getColor(R.styleable.GradientRoundCornerTextView_secondaryBackColor, ColorUtils.getDarkerColor(backColor));
        switch (a.getInt(R.styleable.GradientRoundCornerTextView_gradientOrientation, 0)) {
            case 1:
                gradientOrientation = GradientDrawable.Orientation.TOP_BOTTOM;
                break;
            default:
                gradientOrientation = GradientDrawable.Orientation.LEFT_RIGHT;
        }
        a.recycle();
    }

    public RoundCornerTextView setBackColor(@ColorInt int backColor) {
        setBackColor(backColor, ColorUtils.getDarkerColor(backColor));
        return this;
    }

    public void setBackColor(@ColorInt int backColor, @ColorInt int secondaryBackColor) {
        this.backColor = backColor;
        this.secondaryBackColor = secondaryBackColor;
    }

    public void refreshBackground() {
        if (!isEnabled()) {
            super.refreshBackground();
            return;
        }
        GradientDrawable normalDrawable = new GradientDrawable();
        normalDrawable.setColors(new int[]{backColor, secondaryBackColor});
        normalDrawable.setCornerRadii(new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        });
        normalDrawable.setOrientation(gradientOrientation);
        normalDrawable.setGradientRadius(0);
        normalDrawable.setStroke(borderWidth, borderColor);
        setBackground(normalDrawable);
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            setTranslationZ(0);
//            setElevation(0);
//            //获取outLine，我们需要使用ViewoutLineProvider
//            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
//                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
//                @Override
//                public void getOutline(View view, Outline outline) {
////                    //修改outLine的形状，这里是设置分别设置左上右下，以及Radius
////                    outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),30);
//                }
//            };
//            //将需要控件重写设置形状
//            setOutlineProvider(viewOutlineProvider);
//        }
    }
}
